/**
* A*算法程序实现
* open队列
* 排序估价函数
* close队列
* 排除干扰点
* 查询相邻位置
* 封装估价函数 f() g() h() 
* 设置父节点指针
*/
var map = [
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 1, 3, 0, 0, 3, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 3, 0, 3, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 3, 0, 3, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0

]
var oUl = document.getElementById('ul')
var aLi = oUl.getElementsByTagName('li')
var size = 20
var num = Math.sqrt(map.length)
init()
var beginLi = document.querySelector('a')
var endLi = document.querySelector('b')
var openArr = [],closeArr = [] // 可能要走路线的队列，关闭路线的队列

function init(){
    createMap()
}

function createMap() {
    oUl.style.width = num * (size + 1) + 1 + 'px'
    map.map(function (item) {
        var li = document.createElement('li')
        li.style.width = li.style.height = size + 'px'
        if (item === 1) {
            li.className = 'a'
            // 其实位置可以走，存入open队列
            openArr.push(item)
        } else if (item === 2) {
            li.className = 'b'
        } else if (item === 3) {
            li.className = 'c'
            // 有障碍的位置不能走，存入close队列
            openArr.push(item)
        }
        oUl.appendChild(li)
    })
}
// 估价函数
function f(li){
    return g(li)+h(li)
}
function g(li){
    // 直角三角行求斜边 a*a+b*b=c*c
    var a = beginLi.offsetLeft - li.offsetLeft
    var b = beginLi.offsetTop - li.offsetTop
    return Math.sqrt(a*a+b*b)
}
function h(){
    var a = endLi.offsetLeft - li.offsetLeft
    var b = endLi.offsetTop - li.offsetTop
    return Math.sqrt(a*a+b*b)
}

//open队列

